package com.ckk.cloud.orderpayment.controller;


import com.ckk.apicommoncloud.entity.CommonResult;
import com.ckk.apicommoncloud.entity.Payment;
import com.ckk.cloud.orderpayment.lb.LoadBlance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author 陈昆昆
 * @version 1.0
 * @date 2020/6/15 15:51
 */
@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBlance loadBlanceImpl;

//    private  static  final String PAYMENT_URL="http://localhost:8001";http://CLOUD-PAYMENT-SERVICE
    private  static  final String PAYMENT_URL="http://CLOUD-ORDER-PROVIDER";
    @PostMapping("consumer/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-ORDER-PROVIDER");
        ServiceInstance serviceInstance = loadBlanceImpl.getServiceInstance(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.postForObject(uri+"/payment/create",payment, CommonResult.class);
    }

//    @GetMapping("consumer/payment/get/{id}")
//    public CommonResult<Payment> get(@PathVariable("id")Long id){
//        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
//    }


    @GetMapping("consumer/payment/get/{id}")
    public CommonResult<Payment> get(@PathVariable("id")Long id){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-ORDER-PROVIDER");
        ServiceInstance serviceInstance = loadBlanceImpl.getServiceInstance(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/payment/get/"+id,CommonResult.class);
    }



    @GetMapping("consumer/payment/getentity/{id}")
    public CommonResult<Payment> getentity(@PathVariable("id")Long id){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-ORDER-PROVIDER");
        ServiceInstance serviceInstance = loadBlanceImpl.getServiceInstance(instances);
        URI uri = serviceInstance.getUri();
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(uri + "/payment/get/" + id, CommonResult.class);
        if (forEntity.getStatusCode().is2xxSuccessful()){
          return forEntity.getBody();
        }else{
          return new CommonResult<>(444,"操作失败");
        }

    }
    @GetMapping("payment/discoryclient")
    public DiscoveryClient getDiscoveryClient(){
        List<String> services = discoveryClient.getServices();
        for (String str:services) {
            System.out.println(str);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-ORDER-PROVIDER");
        for (ServiceInstance instance:instances) {
            System.out.println(instance.getHost()+"-->"+instance.getUri()+"-->"+instance.getPort());
        }
        return this.discoveryClient;
    }

}
